package com.example.demo.controller;

import com.example.demo.api.JsonResult;
import com.example.demo.api.ResultCode;
import com.example.demo.entity.Reservation;
import com.example.demo.entity.Stadium;
import com.example.demo.entity.User;
import com.example.demo.service.AdminService;
//import com.example.demo.service.TokenService;
import com.example.demo.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1")
@CrossOrigin(origins = "*")
public class AdminController {
    @Autowired
    private AdminService adminservice;


    @PostMapping("/admin/token_test")
    public JsonResult loginTest(@RequestBody User user){

        System.out.println("......登录......");
        try {
            User temp = adminservice.findAUsers(user.getUsername());
            //System.out.println(temp);
            if(temp.getPassword().equals(user.getPassword())) {
                if(temp.isAdmin()) {/**管理员登录**/
                    User user1=adminservice.findUserId(user);
                    String token= TokenUtil.generateToken(user1);
                    System.out.println("管理员登录成功\n");
                    return JsonResult.result(ResultCode.A_LOGIN_SUCCESS.getCode(), ResultCode.A_LOGIN_SUCCESS.getMessage(), token);
                } else {/**普通用户登录**/
                    System.out.println("权限不足\n");
                    return JsonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
                }
            } else{/**密码错误**/
                System.out.println("密码错误\n");
                return JsonResult.result(ResultCode.PASSWORD_FAILED.getCode(), ResultCode.PASSWORD_FAILED.getMessage());
            }
        } catch(NullPointerException e) {/**用户不存在**/
            System.out.println("用户不存在\n");
            return JsonResult.result(ResultCode.USERNAME_FAILED.getCode(), ResultCode.USERNAME_FAILED.getMessage());
        } catch (Exception e){
            System.out.println(e.toString()+"\n登陆失败\n");
            return JsonResult.result(ResultCode.LOGIN_FAILED.getCode(), ResultCode.LOGIN_FAILED.getMessage());}

    }

    @DeleteMapping("/admin/token_test2/{username}/{token}")
    public JsonResult logoutTest(@PathVariable(name="token") String token,
                              @PathVariable(name="username") String username) {
//        System.out.println("token: "+token+" , username"+tokenservice.getName(token)+" 退出......");
        try {
            System.out.println(TokenUtil.getUser(token));
            if (TokenUtil.getUser(token).getUsername().equals(username)) {
//                tokenservice.deleteToken(token);
                /**退出成功**/
                System.out.println("退出成功\n");
                return JsonResult.result(ResultCode.LOGOUT_SUCCESS.getCode(), ResultCode.LOGOUT_SUCCESS.getMessage());
            } else {
                /**非法用户**/
                System.out.println("权限不足\n");
                return JsonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {/**退出失败**/
            System.out.println(e.toString()+"\n退出失败\n");
            return JsonResult.result(ResultCode.LOGOUT_FAILED.getCode(), ResultCode.LOGOUT_FAILED.getMessage());
        }
    }





    @PostMapping("/admin/token")
    public JsonResult login2(@RequestBody User user){

        System.out.println("......登录......");
        try {
            User temp = adminservice.findAUsers(user.getUsername());
            //System.out.println(temp);
            if(temp.getPassword().equals(user.getPassword())) {
                if(temp.isAdmin()) {/**管理员登录**/
                    User user1=adminservice.findUserId(user);
                    String token= TokenUtil.generateToken(user1);
                    System.out.println("管理员登录成功\n");
                    return JsonResult.result(ResultCode.A_LOGIN_SUCCESS.getCode(), ResultCode.A_LOGIN_SUCCESS.getMessage(), token);
                } else {/**普通用户登录**/
                    System.out.println("权限不足\n");
                    return JsonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
                }
            } else{/**密码错误**/
                System.out.println("密码错误\n");
                return JsonResult.result(ResultCode.PASSWORD_FAILED.getCode(), ResultCode.PASSWORD_FAILED.getMessage());
            }
        } catch(NullPointerException e) {/**用户不存在**/
            System.out.println("用户不存在\n");
            return JsonResult.result(ResultCode.USERNAME_FAILED.getCode(), ResultCode.USERNAME_FAILED.getMessage());
        } catch (Exception e){
            System.out.println(e.toString()+"\n登陆失败\n");
            return JsonResult.result(ResultCode.LOGIN_FAILED.getCode(), ResultCode.LOGIN_FAILED.getMessage());}

    }
//
//    @DeleteMapping("/admin/token/{username}/{token}")
//    public JsonResult logout2(@PathVariable(name="token") String token,
//                             @PathVariable(name="username") String username) {
//        System.out.println("token: "+token+" , username"+tokenservice.getName(token)+" 退出......");
//        try {
//            if (tokenservice.searchToken(token)&&username.equals(tokenservice.getName(token))) {
//                tokenservice.deleteToken(token);
//                /**退出成功**/
//                System.out.println("退出成功\n");
//                return JsonResult.result(ResultCode.LOGOUT_SUCCESS.getCode(), ResultCode.LOGOUT_SUCCESS.getMessage());
//            } else {
//                /**非法用户**/
//                System.out.println("权限不足\n");
//                return JsonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
//            }
//        } catch (Exception e) {/**退出失败**/
//            System.out.println(e.toString()+"\n退出失败\n");
//            return JsonResult.result(ResultCode.LOGOUT_FAILED.getCode(), ResultCode.LOGOUT_FAILED.getMessage());
//        }
//    }

    //查看所有预定信息
    //增加场地
    //删除场地
    //修改场地状态
    //发布通知

    /**
     * 管理员查看所有场地
     * @return
     */
    @GetMapping("/admin/stadiums/{username}/{type}")
    public JsonResult browse2(@PathVariable(name="username") String username,
                              @PathVariable(name="type") String type) {
        System.out.println("......浏览场地......");
        List<Stadium> stadiumList;
        try {
            if (adminservice.findAUsers(username).isAdmin()) {
                stadiumList = adminservice.findStadiumsByType(type);
                System.out.println(stadiumList.toString());
                System.out.println("查看成功\n");
                return JsonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), stadiumList);

            } else {
                System.out.println("权限不足\n");
                return JsonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("查看失败\n");
            return JsonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }
    }

    /**
     * 管理员查看场地预定信息
     * @return
     */
    @GetMapping("/admin/reservations/{username}/{type}")
    public JsonResult browseItem2 (@PathVariable(name="username") String username,
                                   @PathVariable(name="type") String type) {
        System.out.println("......查看预定记录......");
        List<Reservation> reservationList;
        try {
            if (adminservice.findAUsers(username).isAdmin()) {
                reservationList = adminservice.findReservationsByType(type);
                System.out.println(reservationList.toString());
                System.out.println("查看成功\n");
                return JsonResult.result(ResultCode.GET_INFO_SUCCESS.getCode(), ResultCode.GET_INFO_SUCCESS.getMessage(), reservationList);
            }else{
                System.out.println("权限不足\n");
                return JsonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }

        } catch (Exception e) {
            System.out.println(e);
            System.out.println("查看失败\n");
            return JsonResult.result(ResultCode.GET_INFO_FAILED.getCode(), ResultCode.GET_INFO_FAILED.getMessage());
        }
    }

    /**
     * 管理员增加场地
     * @param username
     * @param stadium
     * @return
     */
    @PostMapping("/admin/stadiums/{username}")
    public JsonResult addStadium (@PathVariable(name="username") String username,
                               @RequestBody Stadium stadium) {
        System.out.println("......增加场地......");
//        reservation.setUsername(username);
        try {
            if (adminservice.findAUsers(username).isAdmin()) {
                if(adminservice.findStadiumIdByNumber(stadium)==0){
                    adminservice.addStadiums(stadium);
                    System.out.println("增加成功\n");
                    return JsonResult.result(ResultCode.ADD_STADIUM_SUCCESS.getCode(), ResultCode.ADD_STADIUM_SUCCESS.getMessage());
                } else {
                    System.out.println("场地号冲突\n");
                    return JsonResult.result(ResultCode.STADIUM_FAILED.getCode(), ResultCode.STADIUM_FAILED.getMessage());
                }
            }else {
                System.out.println("权限不足\n");
                return JsonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("增加失败\n");
            return JsonResult.result(ResultCode.ADD_STADIUM_FAILED.getCode(), ResultCode.ADD_STADIUM_FAILED.getMessage());
        }
    }

    /**
     * 管理员删除场地
     * @param r_id
     * @param username
     * @return
     */
    @DeleteMapping("/admin/stadiums/{username}/{s_id}")
    public JsonResult deleteInfo(@PathVariable(name="s_id") String r_id,
                                 @PathVariable(name="username") String username) {
        System.out.println("......删除场地......");
//        System.out.println(token);
        System.out.println(username);
        try {
            int id = Integer.parseInt(r_id);
            if(adminservice.findAUsers(username).isAdmin()) {
                adminservice.deleteStadium(id);
                System.out.println("删除成功\n");
                return JsonResult.result(ResultCode.DELETE_STADIUM_SUCCESS.getCode(), ResultCode.DELETE_STADIUM_SUCCESS.getMessage());
            }else{
                System.out.println("权限不足\n");
                return JsonResult.result(ResultCode.TOKEN_FAILED.getCode(), ResultCode.TOKEN_FAILED.getMessage());
            }
        } catch (Exception e) {
            System.out.println(e);
            System.out.println("删除失败\n");
            return JsonResult.result(ResultCode.DELETE_STADIUM_FAILED.getCode(), ResultCode.DELETE_STADIUM_FAILED.getMessage());
        }
    }


}